import useStore from "@/store";
import { getSpectatorPersonInfoAPI, getDevicePassInfoAPI } from "@/api";

// 获取用户数据
const getUserData = async (seatNum) => {
  const res = await getSpectatorPersonInfoAPI({ seatNum: seatNum });
  if (res.isResultTrue) {
    toUeMsg("pushUserData", res.data);
  }
};


// 获取设备数据
const getDevicePassInfo = async (deviceName) => {
  const res = await getDevicePassInfoAPI({ deviceName: deviceName });
  if (res.isResultTrue) {
    toUeMsg("GetHoveredEquipment", res.data);
  }
};

ue.interface.OnMessage = (v) => {
  const { app } = useStore();
  if (v.includes("loginState")) {
    const state = v.split(":").pop().trim();
    app.setLoginStatus(state === "1");
  }
  if (v.includes("consoleState")) {
    const state = v.split(":").pop().trim();
    app.setConsoleStatus(state === "1");
  }

  if (v.includes("HoveredEquipment")) {
    //获取设备数据
    const deviceName = v.split(":").pop().trim();
    if (deviceName) {
      getDevicePassInfo(deviceName);
    }
  }
  if (v.includes("SeatNum")) {
    //获取桌位号用户数据
    const seatNum = v.split(":").pop().trim();
    if (seatNum) {
      getUserData(seatNum);
    }
  }

  if (v.includes("BrakeMachine")) {
    //是否展示闸机模块
    const k = v.split(":").pop().trim();
    app.setZjPanel(k !== "0");
    app.setZjId(k !== "0" ? k : "");
  }


  if (v.includes("getAreaUserList")) {
    //获取区域 所有座椅号
    const areaKey = v.split(":").pop().trim();
    const list = app.areaListObj[areaKey] || [];
    // toUeMsg('areaUserList', {area: areaKey, list: list})
    // toUeMsg('areaUserList', JSON.stringify({area: areaKey, list: list}))
    // const obj = {}
    // list.map(item => {
    //     obj[item.data] = item
    // })
    toUeMsg("areaUserList", {
      [areaKey]: list.map((item) => item.data).join(","),
    });
  }

  app.setReviceMsg(v);
};

export const toUeMsg = (key, data) => {
  const { app } = useStore();
  app.setSendMsg(JSON.stringify({ key, data }));
  console.log("给Ue发送消息", key, JSON.stringify(data));
  ue5 && ue5(key, data);
};
